package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStat2CDayMapper;
import cc.rengu.igas.momp.common.entity.TMStat2CDay;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 对客户营销活动单日统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @date 2020/4/14 21:49
 */

public class TMStat2CDayMapperImpl implements TMStat2CDayMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 获取对客户营销活动单日统计表数据数据库操作接口实现
     *
     * @param tmStat2CDay 对客户营销活动单日统计表
     * @return cc.rengu.igas.momp.common.entity.TMStat2CDay 对客户营销活动单日统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/14 21:51
     */
    @Override
    public TMStat2CDay selectToClientMcDayStatistics(TMStat2CDay tmStat2CDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectToClientDayMcStatByUsingPK");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStat2CDay, tmStat2CDay);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取对客户营销活动单日统计表数据!<{}>", tmStat2CDay.getInstId(), iReturnCode);
            return null;
        }

        return tmStat2CDay;
    }

    /**
     * 更新对客营销活动单日统计表额度数据库操作接口实现
     *
     * @param tmStat2CDay 对客户营销活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/23 18:46
     */
    @Override
    public int updateToClientMcDayStatistics(TMStat2CDay tmStat2CDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateToClientMcDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStat2CDay, TMStat2CDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>在<{}>的营销活动<{}>的规则<>的对客户营销活动单日统计表数据失败!<{}>",
                    tmStat2CDay.getInstId(), tmStat2CDay.getTxnDate(), tmStat2CDay.getMcNo(), tmStat2CDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 插入单条对客营销活动单日统计表额度数据库操作接口实现
     *
     * @param tmStat2CDay 对客户营销活动单日统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/9 23:22
     */
    @Override
    public int insertSingleToClientMcDayStatistics(TMStat2CDay tmStat2CDay) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSingleToClientMcDayStatistics");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStat2CDay, TMStat2CDay.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>在<{}>的营销活动<{}>的规则<>的对客户营销活动单日统计表数据失败!<{}>",
                    tmStat2CDay.getInstId(), tmStat2CDay.getTxnDate(), tmStat2CDay.getMcNo(), tmStat2CDay.getRuleIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }
}
